//
//  LiveFooterNode.m
//  EducationLive
//
//  Created by Apple on 2017/10/9.
//  Copyright © 2017年 ZhouHao. All rights reserved.
//

#import "LiveFooterNode.h"
#import "SGAdvertScrollView.h"
#import "LiveListModel.h"

@interface LiveFooterNode ()<SGAdvertScrollViewDelegate>

// UI
@property (nonatomic, weak) ASImageNode *marqueeImageNode;
@property (nonatomic, weak) ASImageNode *marqueeNode;
@property (nonatomic, strong) SGAdvertScrollView *advertScrollView;
// Data
@property (nonatomic, strong) NSArray *dataArray;

@end

@implementation LiveFooterNode

- (instancetype)initWithItem:(NSArray *)dataArray
{
    if (self = [super init]) {
        
        _dataArray = dataArray;
        // 加载图片
        [self addMarqueeImageNode];
        // 加载走马灯
        [self addMarqueeNode];
    }
    return self;
}
// 加载视图
- (void)didLoad
{
    [super didLoad];
    // 加载走马灯
    [self addAdvertScrollView];
}
// layout位置
- (void)layout
{
    [super layout];
    _advertScrollView.frame = _marqueeNode.frame;
}
// 加载图片
- (void)addMarqueeImageNode
{
    ASImageNode *imageNode = [[ASImageNode alloc]init];
    imageNode.image = [UIImage imageNamed:@"icon_notice"];
    imageNode.layerBacked = YES;
    [self addSubnode:imageNode];
    _marqueeImageNode = imageNode;
}
// 加载走马灯
- (void)addMarqueeNode
{
    ASImageNode *marqueeNode = [[ASImageNode alloc]init];
    marqueeNode.backgroundColor = RGB_255(223, 223, 223);
    marqueeNode.layerBacked = YES;
    [self addSubnode:marqueeNode];
    _marqueeNode = marqueeNode;
}
// 加载走马灯
- (void)addAdvertScrollView
{
    SGAdvertScrollView *advertScrollView = [[SGAdvertScrollView alloc]init];
    advertScrollView.signImages = [self marqueeArray:@"image"];
    advertScrollView.titles = [self marqueeArray:@"name"];
    advertScrollView.scrollTimeInterval = 5;
    advertScrollView.delegate = self;
    [self.view addSubview:advertScrollView];
    _advertScrollView = advertScrollView;
}

// 创建走马灯数组
- (NSMutableArray *)marqueeArray:(NSString *)type
{
    NSMutableArray *marqueeArray = [NSMutableArray array];
    [_dataArray enumerateObjectsUsingBlock:^(RnoticeModel *obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if ([type isEqualToString:@"name"]) {
            [marqueeArray addObject:obj.name];
        }else if ([type isEqualToString:@"image"]) {
            [marqueeArray addObject:obj.img];
        }
    }];
    return marqueeArray;
}

- (ASLayoutSpec *)layoutSpecThatFits:(ASSizeRange)constrainedSize
{
    _marqueeNode.style.preferredSize = CGSizeMake(kScreenWidth - 29 * 2 - 10 * 2, 29);
    _marqueeImageNode.style.preferredSize = CGSizeMake(29, 29);
    
    _marqueeNode.style.flexShrink = YES;
    ASStackLayoutSpec *marqueeStackLayout = [ASStackLayoutSpec stackLayoutSpecWithDirection:ASStackLayoutDirectionHorizontal spacing:5 justifyContent:ASStackLayoutJustifyContentStart alignItems:ASStackLayoutAlignItemsCenter children:@[_marqueeImageNode,_marqueeNode]];
    
    ASInsetLayoutSpec *marqueeImageLayout = [ASInsetLayoutSpec insetLayoutSpecWithInsets:UIEdgeInsetsMake(0, 5, 5, 5) child:marqueeStackLayout];
    
    return marqueeImageLayout;
}

#pragma mark - delegate
- (void)advertScrollView:(SGAdvertScrollView *)advertScrollView didSelectedItemAtIndex:(NSInteger)index {
    
}

@end
